/*

 */
package com.ohos.connent.accountcloud.biz.modular.dp.service.impl;

import cn.hutool.core.bean.BeanUtil;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ohos.connent.accountcloud.biz.core.enums.BizDataTypeEnum;
import com.ohos.connent.accountcloud.biz.modular.dp.entity.DpOrg;
import com.ohos.connent.accountcloud.biz.modular.dp.enums.DpOrgCategoryEnum;
import com.ohos.connent.accountcloud.biz.modular.dp.enums.DpOrgStatusEnum;
import com.ohos.connent.accountcloud.biz.modular.dp.mapper.DpOrgMapper;
import com.ohos.connent.accountcloud.biz.modular.dp.param.*;
import com.ohos.connent.accountcloud.biz.modular.dp.service.DpOrgService;

import com.ohos.connent.accountcloud.biz.modular.dp.service.DpUserService;
import com.ohos.connent.accountcloud.common.cache.CommonCacheOperator;
import com.ohos.connent.accountcloud.common.exception.CommonException;
import com.ohos.connent.accountcloud.common.listener.CommonDataChangeEventCenter;
import com.ohos.connent.accountcloud.sys.api.SysRoleApi;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 机构Service接口实现类
 *
 **/
@Service
public class DpOrgServiceImpl extends ServiceImpl<DpOrgMapper, DpOrg> implements DpOrgService {

    public static final String ORG_CACHE_ALL_KEY = "sys-org:all";
    
    @Resource
    private CommonCacheOperator commonCacheOperator;
    
    @Resource
    private SysRoleApi sysRoleApi;

    @Resource
    private DpUserService dpUserService;



    @Transactional(rollbackFor = Exception.class)
    @Override
    public String add(DpOrgAddParam dpOrgAddParam) {
        DpOrgCategoryEnum.validate(dpOrgAddParam.getCategory());
        // 校验数据范围
        /*List<String> loginUserDataScope = StpLoginUserUtil.getLoginUserDataScope();
        if(ObjectUtil.isNotEmpty(loginUserDataScope)) {
            if(!loginUserDataScope.contains(dpOrgAddParam.getParentId())) {
                throw new CommonException("您没有权限在该机构下增加机构，机构id：{}", dpOrgAddParam.getParentId());
            }
        } else {
            throw new CommonException("您没有权限增加机构");
        }*/
        DpOrg dpOrg = BeanUtil.toBean(dpOrgAddParam, DpOrg.class);

        // 重复名称
        boolean repeatName = this.count(new LambdaQueryWrapper<DpOrg>().eq(DpOrg::getParentId, dpOrg.getParentId())
                .eq(DpOrg::getName, dpOrg.getName())) > 0;
        if(repeatName) {
            throw new CommonException("存在重复的同级机构，名称为：{}", dpOrg.getName());
        }
        dpOrg.setCode(RandomUtil.randomString(10));
        dpOrg.setStatus(DpOrgStatusEnum.PENDING_REVIEW.getStatus());
        this.save(dpOrg);

        // 发布增加事件
        CommonDataChangeEventCenter.doAddWithData(BizDataTypeEnum.ORG.getValue(), JSONUtil.createArray().put(dpOrg));
        return dpOrg.getId();
    }

    @Override
    public List<DpOrg> getOrgListByCode(List<String> codes) {
        return this.list(Wrappers.lambdaQuery(DpOrg.class).in(DpOrg::getCode, codes));
    }

    @Override
    public DpOrg queryEntity(String id) {
        DpOrg dpOrg = this.getById(id);
        if(ObjectUtil.isEmpty(dpOrg)) {
            throw new CommonException("机构不存在，id值为：{}", id);
        }
        return dpOrg;
    }

    @Override
    public List<DpOrg> getAllOrgList() {
        return this.list(new LambdaQueryWrapper<DpOrg>().orderByAsc(DpOrg::getSortCode));
    }

}
